package com.ocom.oauth.auth.wx;

import com.ocom.common.utils.CommonUtil;
import com.ocom.oauth.auth.CustomAbstractTokenGranter;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.Map;

/**
 *
 *  说明：
 *    验证码
 *    定义验证码具体逻辑
 */
public class WxTokenGranter extends CustomAbstractTokenGranter {

    private static final String CODE_AUTH = "wx_auth";

    private WxUserDetailService wxUserDetailService;

    public WxTokenGranter(AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory,WxUserDetailService wxUserDetailService) {
        super(tokenServices, clientDetailsService, requestFactory, CODE_AUTH);
        this.wxUserDetailService = wxUserDetailService;
    }

    @Override
    protected UserDetails getUserDetails(Map<String, String> parameters) {
        String wx_code = parameters.get("wx_code");
        String from = parameters.get("from");
        if(CommonUtil.isNull(from)){
            from = "xcx" ;
        }
        return wxUserDetailService.loadByCode(wx_code, from);
    }
}
